﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace wpf_ImageSearch.Model.YahooApis
{
    public class ImageSearchResult : IEnumerable<ImageSearchResultItem>
    {
        public ImageSearchResult(string responseXml)
        {
            var xml = XElement.Parse(responseXml);

            if (xml.Name.LocalName == "Error")
            {
                ErrorMessage = xml.Elements().First().Value;//Message要素
                return;
            }

            TotalResultsAvailable = long.Parse(xml.Attribute("totalResultsAvailable").Value);
            TotalResultsReturned = int.Parse(xml.Attribute("totalResultsReturned").Value);
            FirstResultPosition = int.Parse(xml.Attribute("firstResultPosition").Value);

            items = xml.Elements()
                .Select(_ =>
                {
                    var item = new ImageSearchResultItem();

                    foreach (var itemField in _.Elements())
                    {
                        var value = itemField.Value;
                        switch (itemField.Name.LocalName)
                        {
                            case "Title":
                                item.Title = value;
                                break;
                            case "Summary":
                                item.Summary = value;
                                break;
                            case "Url":
                                item.Url = value;
                                break;
                            case "ClickUrl":
                                item.ClickUrl = value;
                                break;
                            case "RefererUrl":
                                item.RefererUrl = value;
                                break;
                            case "FileSize":
                                item.FileSize = value;
                                break;
                            case "FileFormat":
                                item.FileFormat = value;
                                break;
                            case "Height":
                                item.Height = int.Parse(value);
                                break;
                            case "Width":
                                item.Width = int.Parse(value);
                                break;
                            case "Thumbnail":
                                item.Thumbnail = new ThumbnailInfo(itemField);
                                break;
                        }
                    }

                    return item;
                })
                .ToArray();
        }

        #region IEnumerableの実装
        public IEnumerator<ImageSearchResultItem> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        #endregion

        private IList<ImageSearchResultItem> items = new ImageSearchResultItem[] { };

        public string ErrorMessage { protected set; get; }

        public long TotalResultsAvailable { protected set; get; }
        public int TotalResultsReturned { protected set; get; }
        public int FirstResultPosition { protected set; get; }

        public ImageSearchResultItem this[int index]
        {
            get
            {
                return items[index];
            }
        }
    }
}
